//
//  YHStatusCell.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/6/28.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit
import SnapKit

let StautsMargin: CGFloat = 10

class YHStatusCell: UITableViewCell {
    
    //记录底部toolBar顶部的约束
    var toolBarConstraint:Constraint?
    
    //供外界给cell设置数据
    var status:YUStatus?{
        didSet{
            //给原装微博赋值
            statusOriginalView.status = status
            
            //先卸载底部toolBar top 原有约束
            toolBarConstraint?.uninstall()
            //没有转发微博
            if status?.retweeted_status == nil{
            
                statusRetweetView.hidden = true
                //重新设置约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    toolBarConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
            }else{
                
                //有转发微博,先赋值
                statusRetweetView.status = status
                statusRetweetView.hidden = false
                
                statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                    toolBarConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                })
            }
            //给toolBar赋值
            statusToolBar.status = status
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(statusRetweetView)
        
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
//            make.height.equalTo(50)
        }
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            toolBarConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
        }
        
        
    }
    
    private lazy var statusOriginalView:YHStatusOriginalView = YHStatusOriginalView()
    
    private lazy var statusToolBar:YHStatusToolBar = YHStatusToolBar()
    
    private lazy var statusRetweetView:YHStatusRetweetView = YHStatusRetweetView()
    
    private lazy var pictureView:YHPictureView = YHPictureView()
    
}









